package com.example.coroutines_test.adapter

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.appcompat.app.AppCompatViewInflater
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView

class MyListAdapter(val data: List<String>) :
    ListAdapter<String, MyListAdapter.Holder>(CheckItemCallback()),
    Parcelable {



    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): Holder {

        //AppCompatViewInflater().createView()

        return Holder(TextView(p0.context))
    }

    override fun getItemCount(): Int {
        // 不实现这个方法会导致 整个列表不显示
        // 具体原理待分析
        return data.size
    }

    override fun onBindViewHolder(p0: Holder, p1: Int) {
        p0.bind(data[p1])
    }


    class Holder(val view: TextView) : RecyclerView.ViewHolder(view) {
        fun bind(data: String) {
            view.text = data
        }

    }

    class CheckItemCallback : DiffUtil.ItemCallback<String>() {
        override fun areItemsTheSame(p0: String, p1: String): Boolean {
            return p0 == p1
        }

        override fun areContentsTheSame(p0: String, p1: String): Boolean {
            return p0 == p1
        }
    }

    override fun describeContents(): Int {
        TODO("Not yet implemented")
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        TODO("Not yet implemented")
    }

    companion object CREATOR : Parcelable.Creator<MyListAdapter> {
        override fun createFromParcel(parcel: Parcel): MyListAdapter {
            TODO("Not yet implemented")
        }

        override fun newArray(size: Int): Array<MyListAdapter?> {
            return arrayOfNulls(size)
        }
    }
}